package pl.edu.agh.ssm.monitor.data;

import java.net.InetAddress;

import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;


/**
 * Ours wrapper for jpcap.Packet class.
 * It's needed for easily retrieving informations
 * like addresses or destination port
 * @author Tomasz Jadczyk
 *
 */
public class Packet {

	private InetAddress sourceAddress;
	private int sourcePort;
	private InetAddress destAddress;
	private int destPort;
	
	private byte[] data;
	
	public Packet() {
		
	}
	
	public Packet(jpcap.packet.Packet jpcapPacket) {
		if (jpcapPacket instanceof UDPPacket) {
			UDPPacket udpPacket = (UDPPacket) jpcapPacket;
			retrieveInformationsFromUDPPacket(udpPacket);
		}
		if (jpcapPacket instanceof TCPPacket) {
			TCPPacket tcpPacket = (TCPPacket) jpcapPacket;
			retrieveInformationsFromTCPPacket(tcpPacket);
		}
	}
	
	public Packet(jpcap.packet.UDPPacket udpPacket) {
		retrieveInformationsFromUDPPacket(udpPacket);
	}
	
	public Packet(jpcap.packet.TCPPacket tcpPacket) {
		retrieveInformationsFromTCPPacket(tcpPacket);
	}
	
	public InetAddress getSourceAddress() {
		return sourceAddress;
	}
	public void setSourceAddress(InetAddress sourceAddress) {
		this.sourceAddress = sourceAddress;
	}
	public InetAddress getDestAddress() {
		return destAddress;
	}
	public void setDestAddress(InetAddress destAddress) {
		this.destAddress = destAddress;
	}
	public int getDestPort() {
		return destPort;
	}
	public void setDestPort(int destPort) {
		this.destPort = destPort;
	}
	public byte[] getData() {
		return data;
	}
	public void setData(byte[] data) {
		this.data = data;
	}
	
	public int getSourcePort() {
		return sourcePort;
	}

	public void setSourcePort(int sourcePort) {
		this.sourcePort = sourcePort;
	}

	private void retrieveInformationsFromUDPPacket(UDPPacket udpPacket) {
		this.sourceAddress = udpPacket.src_ip;
		this.sourcePort = udpPacket.src_port;
		this.destAddress = udpPacket.dst_ip;
		this.destPort = udpPacket.dst_port;
		this.data = udpPacket.data;
	}
	
	private void retrieveInformationsFromTCPPacket(TCPPacket tcpPacket) {
		this.sourceAddress = tcpPacket.src_ip;
		this.sourcePort = tcpPacket.src_port;
		this.destAddress = tcpPacket.dst_ip;
		this.destPort = tcpPacket.dst_port;
		this.data = tcpPacket.data;
	}
	
}
